package com.sky.aspect;
import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 自定义切面，实现公共字段自动填充处理逻辑
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    /**
     * 切入点
     */
    //@Pointcut(value = "@annotation(com.sky.aop.AutoFill)")
    /*@Pointcut(value = "@annotation(com.sky.aop.AutoFill)")
    public void autoFillPointCut(){}*/

    /**
     * 前置通知，在通知中进行公共字段的赋值
     */
    @Before("execution(* com.sky.mapper.admin.*.*(..))&& @annotation(com.sky.annotation.AutoFill) ")
    public void autoFill(JoinPoint joinPoint){
        log.info("开始进行公共字段自动填充...");
        //获取实体
        Object entity = joinPoint.getArgs()[0];
        //判断被切入的方法是什么
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取被切入点的方法
        AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class);
        OperationType operationType = autoFill.value();

        try {
            if(operationType==OperationType.INSERT){
                Class<?> clazz = entity.getClass();
                Method setCreateUser = clazz.getMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateUser = clazz.getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method setCreateTime = clazz.getMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setUpdateTime = clazz.getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                //调用方法
                setCreateUser.invoke(entity,BaseContext.getCurrentId());
                setUpdateUser.invoke(entity,BaseContext.getCurrentId());
                setCreateTime.invoke(entity,BaseContext.getCurrentId());
                setUpdateTime.invoke(entity,BaseContext.getCurrentId());
            }else if(operationType==OperationType.UPDATE){
                Class<?> clazz = entity.getClass();
                Method setUpdateUser = clazz.getMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                Method setUpdateTime = clazz.getMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                //调用方法
                setUpdateUser.invoke(entity,BaseContext.getCurrentId());
                setUpdateTime.invoke(entity,BaseContext.getCurrentId());
            }
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }

    }
}
